package com.sikaryofficial.backend.service.store;

import com.sikaryofficial.backend.config.StoreCommentConfig;
import com.sikaryofficial.backend.constant.RedisCacheKey;
import com.sikaryofficial.backend.service.IStoreCommentLimitService;
import com.sikaryofficial.common.redis.service.RedisService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Objects;

/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-08-22
 */

@Service
public class StoreCommentLimitService implements IStoreCommentLimitService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private StoreCommentConfig storeCommentConfig;

    @Override
    public boolean allowComment(Long storeId) {
        String key = MessageFormat.format(RedisCacheKey.STORE_USER_COMMENT_POINTS_NUM, SecurityUtils.getUserId(), storeId);
        Integer count = redisService.getCacheObject(key);
        if (Objects.nonNull(count) && count == 1) {
          redisService.expire(key, storeCommentConfig.getLimitPeriod());
        }
        return count != null && count <= storeCommentConfig.getLimitCount();
    }
}
